package com.engine4cloud.tdf.supervision.convert;

import com.engine4cloud.tdf.admin.api.entity.SysDept;
import com.engine4cloud.tdf.common.core.constant.SupervisionBusinessConstants;
import com.engine4cloud.tdf.common.core.util.lambda.Actions;
import com.engine4cloud.tdf.supervision.dto.create.GzryFunctionaryCreateDTO;
import com.engine4cloud.tdf.supervision.dto.result.DictSelectResultDTO;
import com.engine4cloud.tdf.supervision.dto.result.GzryFunctionaryResultDTO;
import com.engine4cloud.tdf.supervision.dto.update.GzryFunctionaryUpdateDTO;
import com.engine4cloud.tdf.supervision.entity.GzryFunctionaryEntity;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


@Mapper
public interface GzryFunctionaryConvert {
    GzryFunctionaryConvert INSTANCE = Mappers.getMapper(GzryFunctionaryConvert.class);

    GzryFunctionaryEntity createTo(GzryFunctionaryCreateDTO s, Long tenantId);

    GzryFunctionaryEntity updateTo(GzryFunctionaryUpdateDTO s);

    GzryFunctionaryResultDTO convertTo(GzryFunctionaryEntity s);

    default GzryFunctionaryEntity create(GzryFunctionaryCreateDTO s, SysDept dept, Long tenantId) {
        GzryFunctionaryEntity entity = createTo(s, tenantId);
        entity.setZw(s.getPositionList().get(s.getPositionList().size() - 1));
        entity.setDeptName(dept.getName());
        entity.setDeptId(dept.getDeptId());
        entity.setSsjg(dept.getOrgCode());
        entity.setSjhm(s.getSj());
        additionalArea(entity, s.getAreaList());
        return entity;
    }

    default GzryFunctionaryEntity update(GzryFunctionaryUpdateDTO s) {
        GzryFunctionaryEntity entity = updateTo(s);
        entity.setZw(s.getPositionList().get(s.getPositionList().size() - 1));
        entity.setSjhm(s.getSj());
        additionalArea(entity, s.getAreaList());
        return entity;
    }

    default GzryFunctionaryResultDTO convert(GzryFunctionaryEntity s, List<DictSelectResultDTO> dctPositionList) {
        if (Objects.isNull(s) || CollectionUtils.isEmpty(dctPositionList)) {
            return null;
        }

        GzryFunctionaryResultDTO result = convertTo(s);
        result.setSj(s.getSjhm());
        String parent = dctPositionList.stream().filter(position -> Objects.equals(position.getCode(), s.getZw())).map(DictSelectResultDTO::getPcode).findFirst().orElse(null);

        List<String> positionList = new ArrayList<>();
        Actions.apply(StringUtils.isNotEmpty(parent), () -> positionList.add(parent));
        positionList.add(s.getZw());

        result.setPositionList(positionList);
        renderAreaList(s, result);
        return result;
    }

    default void additionalArea(GzryFunctionaryEntity s, List<String> areaList) {
        if (Objects.isNull(s) || CollectionUtils.isEmpty(areaList)) {
            return;
        }
        s.setJtzzszs(areaList.get(SupervisionBusinessConstants.AREA_PROVINCE_INDEX));
        s.setJtzzszds(areaList.get(SupervisionBusinessConstants.AREA_CITY_INDEX));
        if (areaList.size() >= SupervisionBusinessConstants.AREA_COUNTY_ARRAY_LENGTH) {
            s.setJtzzszxq(areaList.get(SupervisionBusinessConstants.AREA_COUNTY_INDEX));
        } else {
            s.setJtzzszxq(null);
        }

        if (areaList.size() >= SupervisionBusinessConstants.AREA_VILLAGE_ARRAY_LENGTH) {
            s.setJtzzszd(areaList.get(SupervisionBusinessConstants.AREA_VILLAGE_INDEX));
        } else {
            s.setJtzzszd(null);
        }
    }

    default void renderAreaList(GzryFunctionaryEntity s, GzryFunctionaryResultDTO t) {
        if (Objects.isNull(s) || Objects.isNull(t)) {
            return;
        }

        List<String> areaList = new ArrayList<>();
        Actions.apply(StringUtils.isNotEmpty(s.getJtzzszs()), () -> areaList.add(s.getJtzzszs()));
        Actions.apply(StringUtils.isNotEmpty(s.getJtzzszds()), () -> areaList.add(s.getJtzzszds()));
        Actions.apply(StringUtils.isNotEmpty(s.getJtzzszxq()), () -> areaList.add(s.getJtzzszxq()));
        Actions.apply(StringUtils.isNotEmpty(s.getJtzzszd()), () -> areaList.add(s.getJtzzszd()));

        t.setAreaList(areaList);
    }

}
